package com.gzhryc.shared_device.oem.code.member;

import java.util.Date;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.common.payment.dao.models.CashWithdrawalConfig;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberTransferMoneyOrder;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberTransferMoneyOrderEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberTransferMoneyOrderService;

/**
 * 会员提现自动处理
 */
public class MemberTransferMoenyAutoHandle implements Runnable{
	
	static Logger log = Logger.getLogger(MemberTransferMoenyAutoHandle.class);
	
	private String tradeNo;
	private Date createDate;
	private CashWithdrawalConfig config;
	private OperateInfo operateInfo;
	
	public MemberTransferMoenyAutoHandle(CashWithdrawalConfig config,String tradeNo,Date createDate,OperateInfo operateInfo) {
		this.config = config;
		this.tradeNo = tradeNo;
		this.createDate = createDate;
		this.operateInfo = operateInfo;
	}

	@Override
	public void run() {
		if(StringTools.isNotBlank(tradeNo)) {
			MemberTransferMoneyOrder transferMoneyOrder = MemberTransferMoneyOrderService.self(createDate)
					.getByTradeNo(this.tradeNo);
			if(transferMoneyOrder != null && MemberTransferMoneyOrderEnum.State.Check.index()
					.equals(transferMoneyOrder.getState())) {
				if(config.getAutoMaxMoney() > 0 && config.getAutoMaxMoney() >=transferMoneyOrder.getMoney()) {
					
		            operateInfo.setOperator( EOperatorType.System.index(),EOperatorType.System.note());
		            operateInfo.setOperateNote("自动处理（已退款方式提现）");
		            
		            try {
		                if (!MemberTransferMoneyOrderService.self(createDate).manualProcessing(transferMoneyOrder
		                		,MemberTransferMoneyOrderEnum.HandleType.RefundProcessing.index()
		                		,PayServiceFactory.self(),operateInfo)) {
		                	log.error("会员提现自动处理失败，订单：{{0:json}}", transferMoneyOrder);
		                }
		            } catch (LogicException e) {
		            	log.error(e.getMessage(), e);
		            }
				}
			}
		}
	}

}
